//---------------------------------------------------------------------
// Author: Alex K. Angelopoulos
//
// Description: Start a new process.
//
// Creation Date: 2006-09-25
//---------------------------------------------------------------------
using System;
using System.ComponentModel;
using System.Management.Automation;

namespace Pscx.Commands
{
    using Interop;

    [Cmdlet(VerbsLifecycle.Stop, Pscx.Interop.TerminalServices.SessionNoun),
     Description("Logs off a specific remote desktop session on a system running Terminal Services/Remote Desktop")]
    public class StopRemoteDesktopCommand : Cmdlet
    {
        private TerminalServices _terminalServices;
        private string _computerName = string.Empty;
        private int _id;
        private SwitchParameter _wait;

        [Parameter(HelpMessage = "System where the session to be terminated is running."),
         AcceptsWildcards(false)]
        public string ComputerName
        {
            get { return _computerName; }
            set { _computerName = value; }
        } 

        [Alias("SessionId"),
         Parameter(Position = 1, Mandatory = true, 
                   HelpMessage = "Unique identifier for the session to be logged off.")]
        public int Id
        {
            get { return _id; }
            set { _id = value; }
        } 

        [Parameter(HelpMessage = "Wait for results before returning.")]
        public SwitchParameter Wait
        {
            get { return _wait; }
            set { _wait = value.ToBool(); }
        } 

        protected override void BeginProcessing()
        {
            this._terminalServices = new TerminalServices();
            this._terminalServices.ComputerName = this._computerName;
        }

        protected override void ProcessRecord()
        {
            WriteObject(this._terminalServices.LogoffSession(_id, _wait));
        }
    }
}